<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('入库管理列表')" />
    <th:block th:include="include :: bootstrap-editable-css" />
    <style>
        font-family{

        }
    </style>
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 hidden search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>提货单号：</label>
                                <input type="text" name="orderNo"/>
                            </li>
                            <li hidden="hidden" >
                                <label>产品id：</label>
                                <input type="text" name="productId"/>
                            </li>
                            <li hidden="hidden">
                                <label>供应商ID：</label>
                                <input type="text" name="supplierId"/>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm " id="toolbar" role="group">
                <a class="btn btn-success hidden" onclick="$.operate.add()" shiro:hasPermission="warehouse:orderDetail:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="openEdit()" shiro:hasPermission="warehouse:orderDetail:edit">
                    <i class="fa fa-edit"></i> 入库
                </a>
                <a class="btn btn-danger multiple disabled hidden" onclick="$.operate.removeAll()" shiro:hasPermission="warehouse:orderDetail:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning hidden" onclick="$.table.exportExcel()" shiro:hasPermission="warehouse:orderDetail:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table-store"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
     <th:block th:include="include :: bootstrap-table-editable-js" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('warehouse:orderDetail:edit')}]];
        var removeFlag = [[${@permission.hasPermi('warehouse:orderDetail:remove')}]];
        var statusDatas = [[${@dict.getType('b_order_detail_status')}]];

        var prefix = ctx + "contract/contractProduct";
        let contractId=[[${contractId}]];
        let orderId=[[${orderId}]];
        let contractType=[[${contractType}]];
        let roleFlag=[[${roleFlag}]];

        let chooseCheckBox=[];

        let url=prefix+"/list?"
        if(!isEmptyString(contractId)){
            url=url+"&contractId="+contractId;
            $(".btn-primary").addClass("hidden");

        }
        $(function() {
            var options = {
                id:"bootstrap-table-store",
                url: url,
                onEditableSave: onEditableSave,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sortName: "contractProductId",
                sortOrder:"desc",
                modalName: "入库",
                onCheck: onCheck,
                onUncheck: onUncheck,
                onCheckAll: onCheckAll,
                onUncheckAll: onUncheckAll,
                columns: [
                    {
                        checkbox: true
                    },
                    {
                        field: 'contractProductId',
                        title: 'ID',
                        visible: false
                    },
                    {
                        field: 'productId',
                        title: '产品ID',
                        visible: false
                    },
                    {
                        field: 'contractId',
                        title: '合同Id',
                    },
                    {
                        field: 'name',
                        title: '产品名称',
                    },
                    {
                        field: 'code',
                        title: '产品编码',
                    },
                    {
                        field: 'price',
                        title: '价格'
                    },
                    {
                        field: 'num',
                        title: '总数量'
                    },
                    {
                        field: 'useNum',
                        title: '已库数量',
                    },
                    {
                        field: 'remainNum',
                        title: '待入库数量',
                    },

                    {
                        field : 'operationNum',
                        title : '本次入库',
                        editable : {
                            type : 'text',
                            title : '本次入库数量',
                            validate : function(value) {
                                if (value.length == 0) {
                                    return '本次入库数量不能为空';
                                }
                            }
                        }
                    },
                    {
                        title: '操作',
                        align: 'center',
                        formatter: function(value, row, index) {
                            var actions = [];
                            if(Number(row.remainNum)==0){
                                actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openStoreDetail(\'' + row.contractProductId + '\')"><i class="fa fa-search1"></i>已经完成入库</a> ');

                            }else{
                                actions.push('<a class="btn btn-dark btn-xs " href="javascript:void(0)" onclick="openStoreDetail(\'' + row.contractProductId + '\')"><i class="fa fa-search1"></i>入库详情</a> ');

                            }
                            return actions.join('');
                        }
                    }
                ]
            };
            $.table.init(options);
        });


        function onCheck(row, $element){
            chooseCheckBox.push(row);
            // alert("onCheck  选中行userId：" + row.userId + " userName：" + row.userName);
        }

        function onUncheck(row, $element){
            chooseCheckBox= chooseCheckBox.filter(item=>item !=row)
            // alert("onUncheck 取消行userId：" + row.userId + " userName：" + row.userName);
        }

        function onCheckAll(rowsAfter, rowsBefore){
            // var rows = $.map(rowsAfter, function(row) {
            //     return $.common.getItemField(row, "userId");
            // });
            chooseCheckBox=rowsAfter;
            // alert("全选行：" + rows);
        }

        function onUncheckAll(rowsAfter, rowsBefore){
            /* var rows = $.map(rowsBefore, function(row) {
                 return $.common.getItemField(row, "userId");
             });*/
            chooseCheckBox=[];
            // alert("取消行：" + rows);
        }

        function openEdit(id){
            var data = $("#bootstrap-table-store").bootstrapTable('getSelections');
            var id=data[0].orderDetailId;
            $.modal.open("入库",ctx+"warehouse/orderDetail/edit/"+id+"/"+contractType,1200,($(window).height() - 50),function (index, layero) {
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.submitHandler(index, layero);
                setTimeout(function () {
                    $.table.refresh();
                },800);

            });
        }
        function onEditableSave (field, row, rowIndex, oldValue, $el) {
            if(Number(row.operationNum)>Number(row.remainNum)){
                $.modal.alertWarning("【本次入库数量："+row.operationNum+"】 不能大于 【未入库数量:"+row.remainNum+"】");
                row["operationNum"]=oldValue;
                return;
            }

            //alert("字段名：" + field + "，当前值：" + row[field]  + "，旧值：" + oldValue);
        }
        function openStoreDetail(orderDetailId){
            openWindOptions("查看入库详情",ctx+"warehouse/mallStoreDetail?orderDetailId="+orderDetailId)
        }
        function getAllStoreData(){
            var data = chooseCheckBox;//$("#bootstrap-table-store").bootstrapTable('getSelections');
            if(data.length==0){
                $.modal.alertWarning("请选择入库详情");
                return;
            }
            return JSON.stringify(data);
        }

        function getContractId(){
            return contractId;
        }
    </script>
</body>
</html>
